package com.oyjd.business.sys.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.oyjd.bean.po.Link;
import com.oyjd.bean.po.LoginLog;
import com.oyjd.bean.po.Menu;
import com.oyjd.bean.po.Role;
import com.oyjd.bean.po.RoleOperate;
import com.oyjd.bean.po.SysOperate;
import com.oyjd.bean.po.User;
import com.oyjd.bean.vo.LoginLogVO;
import com.oyjd.bean.vo.MenuVO;
import com.oyjd.bean.vo.SysOperateVO;
import com.oyjd.bean.vo.UserVO;
import com.oyjd.fw.action.Page;
import com.oyjd.fw.dao.BaseDao;

/**
 * 
 * 后台系统模块
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
public class SysDao extends BaseDao {
	/**
	 * 根据帐号获取用户信息
	 * 
	 * @param account
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public UserVO getUser(String account) {
		UserVO result = null;
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(UserVO.class.getName()).append(UserVO.STRUCT);
		sb.append(" from ").append(User.class.getName()).append(" user");
		sb.append(" where user.account=?");
		Query q = getSession().createQuery(sb.toString());
		q.setParameter(0, account);
		List<UserVO> list = q.list();
		if (list.size() > 0) {
			result = list.get(0);
		}
		return result;
	}

	/**
	 * 获取所有Menu
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<MenuVO> getAllMenu() {
		StringBuffer sb = new StringBuffer();
		sb.append("select new ").append(MenuVO.class.getName()).append(MenuVO.STRUCT);
		sb.append(" from ").append(Menu.class.getName()).append(" menu");
		Query q = getSession().createQuery(sb.toString());
		return q.list();
	}

	/**
	 * 根据角色对应的链接
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, List<String>> getLinks() {
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		StringBuffer sb = new StringBuffer();
		sb.append("select ro.roleId,link.link");
		sb.append(" from ").append(Link.class.getName()).append(" ").append("link").append(",").append(RoleOperate.class.getName()).append(" ro").append(",").append(Role.class.getName()).append(" r");
		sb.append(" where r.status=0 and r.roleId=ro.roleId and link.operateId=ro.operateId");
		Query q = getSession().createQuery(sb.toString());
		List<Object[]> tmpList = q.list();
		for (Object[] o : tmpList) {
			String key = (String) o[0];
			if (map.containsKey(key)) {
				map.get(key).add((String) o[1]);
			} else {
				List<String> tmp = new ArrayList<String>();
				tmp.add((String) o[1]);
				map.put(key, tmp);
			}
		}
		return map;
	}

	/**
	 * 获取角色对应模块操作
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, List<SysOperateVO>> getModuleOp() {
		Map<String, List<SysOperateVO>> map = new HashMap<String, List<SysOperateVO>>();
		StringBuffer sb = new StringBuffer();
		sb.append("select r.roleId,sysoperate.operateId,sysoperate.moduleName,sysoperate.operateName,sysoperate.orderKey");
		sb.append(" from ").append(SysOperate.class.getName()).append(" sysoperate,").append(RoleOperate.class.getName()).append(" r");
		sb.append(" where sysoperate.operateId=r.operateId");
		Query q = getSession().createQuery(sb.toString());
		List<Object[]> tmpList = q.list();
		for (Object[] o : tmpList) {
			String key = (String) o[0];
			if (map.containsKey(key)) {
				map.get(key).add(new SysOperateVO((String) o[1], (String) o[2], (String) o[3], (Integer) o[4]));
			} else {
				List<SysOperateVO> tmp = new ArrayList<SysOperateVO>();
				tmp.add(new SysOperateVO((String) o[1], (String) o[2], (String) o[3], (Integer) o[4]));
				map.put(key, tmp);
			}
		}
		return map;
	}

	/**
	 * 查询登入日志列表
	 * 
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page<LoginLogVO> getLoginLog(Page<LoginLogVO> page) {
		String hqlResult = "select new " + LoginLogVO.class.getName() + LoginLogVO.STRUCT;
		String hqlCount = "select count(loginlog)";
		StringBuffer sb = new StringBuffer();
		sb.append(" from ").append(LoginLog.class.getName()).append(" loginlog");
		sb.append(" where 1=1");
		List<Object> ps = new ArrayList<Object>();
		for (Iterator<String> item = page.getParas().keySet().iterator(); item.hasNext();) {
			String key = item.next();
			if ("account".equals(key) || "custIp".equals(key)) {
				sb.append(" and loginlog.").append(key).append(" like ?");
				ps.add("%" + page.getParas().get(key)[0] + "%");
			} else if ("status".equals(key)) {
				sb.append(" and loginlog.").append(key).append("=?");
				ps.add(Integer.parseInt(page.getParas().get(key)[0]));
			} else if ("loginTime".equals(key)) {
				sb.append(" and DATE_FORMAT(loginlog.loginTime,'%Y-%m-%d')=?");
				ps.add(page.getParas().get(key)[0]);
			}
		}
		// do count
		Query qCount = getSession().createQuery(hqlCount + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			qCount.setParameter(i, ps.get(i));
		}
		Long count = (Long) qCount.uniqueResult();
		page.setCount(count);
		// do result
		Query q = getSession().createQuery(hqlResult + sb.toString());
		for (int i = 0; i < ps.size(); i++) {
			q.setParameter(i, ps.get(i));
		}
		q.setFirstResult(page.getFirst());
		q.setMaxResults(page.getPageSize());
		page.setResult(q.list());
		return page;
	}
}
